<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>模块列表</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <link rel="stylesheet" href="./layui_ext/dtree/dtree.css">
        <link rel="stylesheet" href="./layui_ext/dtree/font/dtreefont.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">演示</a>
            <a>
              <cite>导航元素</cite></a>
          </span>
          <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5">
                                <div class="layui-inline layui-show-xs-block">
                                    <input type="text" name="uname"  placeholder="请输入模块名" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
                                </div>
                            </form>
                        </div>
                        <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                            <button class="layui-btn" onclick="xadmin.open('添加模块','./module-add.html',600,400)"><i class="layui-icon"></i>添加</button>
                        </div>
                        <div class="layui-card-body layui-table-body layui-table-main">
                        	<div style="height: 450px;overflow: auto;" id="toolbarDiv">
							  <ul id="demoTree" class="dtree" data-id="0"></ul>
							</div>
                        </div>
                    </div>
                </div>
            </div>
        </div> 
    </body>
    <script type="text/javascript">
	    layui.extend({
	        dtree: '{/}./layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
	      }).use(['dtree','layer','jquery'], function(){
	        var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery;
	        
	        // 初始化树
	        var DemoTree = dtree.render({
	          elem: "#demoTree",//树的id
	          //data: demoTree, // 使用data加载
	          url: "/modules/findAll", // 使用url加载（可与data加载同时存在）
        	  dataStyle: "layuiStyle",  //使用layui风格的数据格式
        	  dataFormat: "list",  //配置data的风格为list
        	  response:{message:"msg",statusCode:0},  //修改response中返回数据的定义
        	  toolbar:true,//开启工具栏
        	  toolbarScroll:"#toolbarDiv",//工具栏滚动
        	  checkbar:true, //开启复选框
        	  record: true, // 开启数据记录模式
        	  toolbarStyle: {//修改节点名
       		    title: "模块",
       		    area: ["50%", "400px"]
       		  },
       		  toolbarBtn:[
       		    [{"label":"路径","name":"url","type":"text"},
       		     {"label":"介绍","name":"details","type":"text"}
       		    ], // 自定义新增页的内容
       		 	[{"label":"路径","name":"url","type":"text"},
       		     {"label":"介绍","name":"details","type":"text"}
       		    ] // 自定义编辑页的内容
       		  ],
        	  toolbarFun: {//工具栏回调函数
        		  	editTreeLoad: function(treeNode){//编辑节点时数据回显
	        		  	var  param = JSON.parse(treeNode.recordData);
	        		  	DemoTree.changeTreeNodeDone(param);
        		    },
        		    addTreeNode: function(treeNode, $div){//添加按钮
        		    	treeNode.nodeId = null;//id置空
        		      	$.ajax({
        		        type: "post",
        		        data: {
        		        	id:treeNode.nodeId,
        		        	title:treeNode.context,
        		        	details:treeNode.details,
        		        	url:treeNode.url,
        		        	parentId:treeNode.parentId
        		        },
        		        url: "/modules/save",
        		        success: function(result){
        		          //DTree1.changeTreeNodeAdd(treeNode.nodeId); // 添加成功，返回ID
        		          	//DemoTree.changeTreeNodeAdd(true); // 添加成功
        		          //DTree1.changeTreeNodeAdd(result.data); // 添加成功，返回一个JSON对象
        		          	DemoTree.changeTreeNodeAdd("refresh"); // 添加成功，局部刷新树
        		        },
        		        error: function(){
        		        	DemoTree.changeTreeNodeAdd(false); // 添加失败
        		        }
        		      }); 
        		    },
        		    editTreeNode: function(treeNode, $div){//编辑按钮
        		      $.ajax({
        		        type: "post",
        		        data: {
        		        	id:treeNode.nodeId,
        		        	title:treeNode.context,
        		        	details:treeNode.details,
        		        	url:treeNode.url,
        		        	parentId:treeNode.parentId
        		        },
        		        url: "/modules/save",
        		        success: function(result){
        		        	DemoTree.changeTreeNodeEdit(true);// 修改成功
        		        	dtree.render({elem: "#demoTree"});//重新渲染树
        		        	//DemoTree.changeTreeNodeEdit("refresh");//局部刷新树，这个方法没有提供，需要自己写
        		          //DTree1.changeTreeNodeEdit(result.param); // 修改成功，返回一个JSON对象
        		        },
        		        error: function(){
        		        	DemoTree.changeTreeNodeEdit(false);//修改失败
        		        }
        		      });
        		    },
        		    delTreeNode: function(treeNode, $div){//删除按钮
        		      $.ajax({
        		        type: "post",
        		        data: {
        		        	id:treeNode.nodeId
        		        },
        		        url: "/modules/delOne",
        		        success: function(result){
        		        	DemoTree.changeTreeNodeDel(true); // 删除成功
        		        },
        		        error: function(){
        		        	DemoTree.changeTreeNodeDel(false);// 删除失败
        		        }
        		      });
        		    }
        		  }
	        });
	        
	        // 绑定节点点击
	        dtree.on("node('demoTree')" ,function(obj){
	          layer.msg(JSON.stringify(obj.param));
	        });
	      });
    </script>
</html>
